package com.example.videodisplay;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.videodisplay.entity.Account;

import org.litepal.LitePal;

import java.util.List;


/**
 *
 * @author xiaotie
 */
public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Account account = new Account();
        account.setPassWord("123");
        account.setUserName("djhgf");
        account.save();
        initView();
    }

    private void initView() {
        Button register = findViewById(R.id.button_register);
        Button login = findViewById(R.id.button_login);
        EditText username = findViewById(R.id.et_username);
        EditText password = findViewById(R.id.et_password);
        CheckBox checkBox = findViewById(R.id.checkBox);
        register.setOnClickListener(v -> startActivity(new Intent(this,RegisterActivity.class)));
        login.setOnClickListener(v -> {
            String user = username.getText().toString().trim();
            String pass = password.getText().toString().trim();
            if(user.isEmpty()) {
                Toast.makeText(this, "账号不能空", Toast.LENGTH_SHORT).show();
            } else if(pass.isEmpty()) {
                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            }else {
                to_login(user,pass);
            }

        });
    }

    private void to_login(String user, String pass) {
        Account accounts = LitePal.where("userName = ?", user).findFirst(Account.class);
        if(accounts==null){
            Toast.makeText(this, "账户不存在", Toast.LENGTH_SHORT).show();
        }else if(accounts.getPassWord().equals(pass)) { 
            startActivity(new Intent(this,ContentActivity.class));
        }else {
            Toast.makeText(this, "账户或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
}















